<?php

namespace app\admin\controller;

use app\common\Helper;

use app\admin\model\User;

/**
 * 用户信息
 */
class UserInfo extends Base
{

    public function getUserInfo()
    {
        $param = self::getHttpParam();
        if (empty($param->appId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'appId 不能为空');
        }
        if (empty($param->openId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'openId 不能为空');
        }

        $appId = $param->appId;
        $openId = $param->openId;
        $user = User::where('openId', $openId)->find();

        if (empty($user)) {
            $user = new User;
            $user->id = Helper::getUUID22();
            $user->openId = $openId;
            $user->save();
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $user);
    }

    /**
     * 保存个人信息
     */
    public function saveUserInfo()
    {
        $param = self::getHttpParam();
        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }

        $user = User::where('id', $param->userId)->find();
        if (!empty($param->username)) {
            $user->username = $param->username;
        }
        if (!empty($param->avatarUrl)) {
            $user->imgUrl = $param->avatarUrl;
        }
        if (!empty($param->sex)) {
            $user->sex = $param->sex;
        }
        if (!empty($param->birthday)) {
            $user->birthday = $param->birthday;
        }

        $result = $user->save();
        if($result === false){
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '保存失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $user);
    }

}